package com.x.chalk.service.impl;

import com.x.chalk.dao.ScorelogDao;
import com.x.chalk.dao.UserSignDao;
import com.x.chalk.entity.Scorelog;
import com.x.chalk.entity.UserSign;
import com.x.chalk.service.intf.UserService;
import com.x.chalk.service.intf.UserSignService;
import com.x.chalk.util.DateUtil;
import com.x.chalk.vo.R;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class UserSignServiceImpl implements UserSignService {

    private final UserSignDao dao;
    private final ScorelogDao scorelogDao;
    private final UserService userService;

    public UserSignServiceImpl(UserSignDao dao, ScorelogDao scorelogDao, UserService userService) {
        this.dao = dao;
        this.scorelogDao = scorelogDao;
        this.userService = userService;
    }

    /**
     * 验证用户今日是否可以签到*/
    @Override
    public R checkSign(String token) {
        UserSign usersign=dao.selectLast(Long.parseLong(token));
        if(usersign==null){
            return R.ok();
        }else {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            if(sdf.format(usersign.getCtime()).equals(sdf.format(new Date()))){
                return R.fail("亲，今日已签到！");
            }else {
                return R.ok();
            }
        }
    }

    @Override
    @Transactional
    public R createSign(String token) {
        long uid=Long.parseLong(token);
        UserSign usersign=dao.selectLast(uid);
        UserSign sign = new UserSign();
        sign.setScore(1);
        sign.setCtime(new Date());
        sign.setDays(1);
        sign.setAccumulateddays(1);
        if(usersign == null){
            sign.setUid(uid);
            sign.setExscore(0);
            sign.setInfo("第一次签到");
        }else{
            int d= DateUtil.diffDay(usersign.getCtime());
            if(d == 0){
                return R.fail("亲，请勿重复签到！");
            }else if(d==1){
                sign.setUid(usersign.getUid());
                sign.setDays(usersign.getDays()+1);
                sign.setExscore(0);
                sign.setAccumulateddays(usersign.getAccumulateddays()+1);
                if(sign.getDays()%30==0){
                    sign.setExscore(30);
                    sign.setInfo("欢迎签到"+sign.getDays()+"天，给予奖励30");
                }else if (sign.getDays()%7==0){
                    sign.setExscore(7);
                    sign.setInfo("欢迎签到"+sign.getDays()+"天，给予奖励7");
                }else{
                    sign.setInfo("欢迎签到"+sign.getDays()+"天，给予奖励1");
                }
            }else{
                sign.setExscore(0);
                sign.setAccumulateddays(usersign.getAccumulateddays()+1);
                sign.setInfo("欢迎签到");
            }
        }


        dao.insert(sign);

        System.out.println((sign.getScore()+sign.getExscore()));

        userService.updateScore(token,(sign.getScore()+sign.getExscore()));

        scorelogDao.insert(new Scorelog(uid, 1, sign.getScore()+sign.getExscore(), "签到奖励积分"));

        return R.ok(sign.getScore()+sign.getExscore());
    }

    @Override
    public R queryDays(String token) {
        return R.ok(dao.queryDays(Long.parseLong(token)));
    }

    @Override
    public R queryAccumulatedDays(String token) {
        return R.ok(dao.queryAccumulatedDays(Long.parseLong(token)));
    }
}
